package com.study.netty.channel;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class BootNettyUdpClientSimpleChannelInboundHandler extends SimpleChannelInboundHandler<DatagramPacket> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
        try {
            String strdata = msg.content().toString(CharsetUtil.UTF_8);
            //打印收到的消息
            log.info("client receive msg: {}", strdata);
            // 与BootNettyUdpClient中的channel.id().toString()是一样的值
            log.info("client channel id: {}", ctx.channel().id().toString());
            //	收到服务端原路返回的消息后，不需要再次向服务端发送消息, 可以在这里暴力关闭，也可以在 channelReadComplete(ChannelHandlerContext ctx)内
            //  ctx.close();
        } catch (Exception e) {

        }
    }

    /**
     * 重写方法
     * 结构：
     * 1.public class BootNettyUdpClientSimpleChannelInboundHandler extends SimpleChannelInboundHandler<DatagramPacket>
     * <p>
     * 2.public abstract class SimpleChannelInboundHandler<I> extends ChannelInboundHandlerAdapter
     * <p>
     * 3.public class ChannelInboundHandlerAdapter extends ChannelHandlerAdapter implements ChannelInboundHandler
     * <p>
     * ChannelInboundHandlerAdapter类有诸多方法可以重写，可以根据具体需求来写
     */

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        super.channelReadComplete(ctx);
        log.info("关闭channel");
        ctx.close();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
